package com.jogern.baselibrary.utils;

import android.text.TextUtils;
import android.util.Log;

/**
 * Log工具类
 * 采用单例，获取对象如：Logcat.log()
 * 打印的格式是：main--> Logcat.java [Method:getFunctionName  line:25]-->msg
 */
public class Logcat {
    private final static boolean isDebug = true;
    private static Logcat log;

    private Logcat() {}

    public static Logcat log() {
        if (log == null){
            synchronized (Logcat.class){
                if (log==null) log = new Logcat();
            }
        }
        return log;
    }

    private String[] getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts == null) {
            return null;
        }
        for (StackTraceElement st : sts) {
            if (st.isNativeMethod()) {
                continue;
            }
            if (st.getClassName().equals(Thread.class.getName())) {
                continue;
            }
            if (st.getClassName().equals(this.getClass().getName())) {
                continue;
            }
            String info = "\t" + Thread.currentThread().getName() + "--> "
                    + st.getFileName() + " [Method:" + st.getMethodName() + "  line:"
                    + st.getLineNumber() + "]-->";
            return new String[]{st.getFileName(), info};
        }
        return null;
    }

    private void logMsg(int logLevel, CharSequence msg) {
        String tag = "Logcat";
        String[] functionName = getFunctionName();
        if (functionName != null) {
            if (!TextUtils.isEmpty(functionName[0])) {
                tag = functionName[0];
                int i = tag.indexOf(".");
                if (i >= 0) {
                    tag = tag.substring(0, i);
                }
            }
            printf(logLevel, tag, functionName[1], msg);
            return;
        }
        printf(logLevel, tag, null, msg);
    }

    private void printf(int logLevel, String tag, String lineInfo, CharSequence msg) {
        switch (logLevel) {
            case Log.VERBOSE:
                Log.v(tag, lineInfo + msg);
                break;
            case Log.DEBUG:
                Log.d(tag, lineInfo + msg);
                break;
            case Log.INFO:
                Log.i(tag, lineInfo + msg);
                break;
            case Log.WARN:
                Log.w(tag, lineInfo + msg);
                break;
            case Log.ERROR:
                Log.e(tag, lineInfo + msg);
                break;
//            case Log.ASSERT:
//                break;
            default:
                Log.d(tag, msg.toString());
        }
    }

    public void i(String str) {
        if (isDebug) logMsg(Log.INFO, str);
    }

    public void d(String str) {
        if (isDebug) logMsg(Log.DEBUG, str);
    }

    public void v(String str) {
        if (isDebug) logMsg(Log.VERBOSE, str);
    }

    public void w(String str) {
        if (isDebug) logMsg(Log.WARN, str);
    }

    public void e(String str) {
        if (isDebug) logMsg(Log.ERROR, str);
    }
}  